<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>任务管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">

        function mytoggle(taskMainId, deviceMainId) {
            var display =$("#devchkItem-" + deviceMainId).css('display');
            if(display == 'none'){
                show(taskMainId, deviceMainId);
                $("#btn-" + deviceMainId).html("隐藏");
            } else {
                hide(deviceMainId);
                $("#btn-" + deviceMainId).html("显示详情");
            }
        }

        function hide(deviceMainId) {
            $("#devchkItem-" + deviceMainId).hide();
        }

        function show(taskMainId, deviceMainId) {
            $("#devchkItem-" + deviceMainId).show();
            var url = "${ctx}/devmng/devchkItem/viewResult?taskMainId="+taskMainId+"&deviceMainId="+deviceMainId;
            $.ajax({
                url: url,
                dataType: 'json',
                success: function(data, textStatus) {
                    debugger;
                    if (!data) {
                        $("#devchkItem-" + deviceMainId).html(wrapTable(wrapTrTd("无任何内容")));
                        return;
                    }
                    if (data.length == 0) {
                        $("#devchkItem-" + deviceMainId).html(wrapTable(wrapTrTd("无任何内容")));
                        return;
                    }
                    var result = "";
                    for (var i = 0; i < data.length; i++) {
                        var content = "检修内容：" + data[i].checkContent + "，检修方法："
                            + data[i].checkMethod + "，检修标准：" + data[i].checkStd
                            + "，检修周期：" + getCheckPeriodDesc(data[i].checkPeriod)
                        result += wrapTrTd(content);
                        if (!data[i].devchkAnswerList || data[i].devchkAnswerList.length == 0) {
                            continue;
                        }
                        for (var j = 0; j < data[i].devchkAnswerList.length; j++) {
                            var devchkAnswer = data[i].devchkAnswerList[j];
                            if (!!devchkAnswer.devchkAnswerItemVoList && devchkAnswer.devchkAnswerItemVoList.length > 0) {
                                if ("single" ==  devchkAnswer.answerType) {
                                    var devchkAnswerItemContent = "";
                                    for (var k = 0; k < devchkAnswer.devchkAnswerItemVoList.length; k++) {
                                        var devchkAnswerItem = devchkAnswer.devchkAnswerItemVoList[k];
                                        if (!!devchkAnswer.taskExecRecordVoList && devchkAnswer.taskExecRecordVoList.length > 0) {
                                            if (devchkAnswer.taskExecRecordVoList[0].devchkAnswerItemId == devchkAnswerItem.id) {
                                                devchkAnswerItemContent += "<input type='radio' checked disabled='true'/>" + devchkAnswerItem.ansItemContent;
                                            } else {
                                                devchkAnswerItemContent += "<input type='radio' disabled='true'  />" + devchkAnswerItem.ansItemContent;
                                            }
                                        } else {
                                            devchkAnswerItemContent += "<input type='radio' disabled='true' />" + devchkAnswerItem.ansItemContent;
                                        }

                                    }
                                    result += wrapTrTd(devchkAnswerItemContent);
                                } else if ("text" ==  devchkAnswer.answerType) {
                                    var devchkAnswerItemContent = "";
                                    var devchkAnswerItem = devchkAnswer.devchkAnswerItemVoList[0];
                                    if (!!devchkAnswer.taskExecRecordVoList && devchkAnswer.taskExecRecordVoList.length > 0) {
                                        devchkAnswerItemContent += devchkAnswerItem.ansItemContent  + "：" + devchkAnswer.taskExecRecordVoList[0].textContent;
                                    } else {
                                        devchkAnswerItemContent += devchkAnswerItem.ansItemContent + "：";
                                    }
                                    result += wrapTrTd(devchkAnswerItemContent);
                                } else if ("number" ==  devchkAnswer.answerType) {
                                    var devchkAnswerItemContent = "";
                                    var devchkAnswerItem = devchkAnswer.devchkAnswerItemVoList[0];
                                    if (!!devchkAnswer.taskExecRecordVoList && devchkAnswer.taskExecRecordVoList.length > 0) {
                                        devchkAnswerItemContent += devchkAnswerItem.ansItemContent  + "：" + devchkAnswer.taskExecRecordVoList[0].numContent;
                                    } else {
                                        devchkAnswerItemContent += devchkAnswerItem.ansItemContent + "：";
                                    }
                                    result += wrapTrTd(devchkAnswerItemContent);
                                }
                            }
                            if (!!devchkAnswer.attaUrlList && devchkAnswer.attaUrlList.length > 0) {
                                var imgContent = "";
                                for (var m = 0; m < devchkAnswer.attaUrlList.length; m++) {
                                    imgContent += wrapImg(devchkAnswer.attaUrlList[m]);
                                }
                                result += wrapTrTd(imgContent);
                            }

                        }
                    }
                    $("#devchkItem-" + deviceMainId).html(wrapTable(result));
                }
            });
        }

        function wrapImg(url) {
            return "<img src='"+ url + "' height='200' width='200' />";
        }

        function wrapTable(content) {
            return "<table class=\"table table-bordered\">" +content + "</table>"
        }

        function wrapTrTd(content) {
            return "<tr><td>" + content + "</td></tr>";
        }

        function getCheckPeriodDesc(code) {
            if (!code) {
                return "未知";
            }
            if (code == "Y") {
                return "年";
            }
            if (code == "M") {
                return "月";
            }
            if (code == "Q") {
                return "季度";
            }
            if (code == "HY") {
                return "半年";
            }
            return "未知";
        }

		$(document).ready(function() {


		});
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/devmng/taskMain/">任务列表</a></li>
		<li class="active"><a href="#">任务详情</a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="taskMain" class="form-horizontal">
		<form:hidden path="id"/>
		<sys:message content="${message}"/>

		<div class="control-group">
			<label class="control-label">开始时间：</label>
			<div class="controls">
				<input name="taskStart" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
					value="<fmt:formatDate value="${taskMain.taskStart}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">结束时间：</label>
			<div class="controls">
				<input name="taskEnd" type="text" readonly="readonly" maxlength="20" class="input-medium Wdate required"
					value="<fmt:formatDate value="${taskMain.taskEnd}" pattern="yyyy-MM-dd HH:mm:ss"/>"
					onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">操作人：</label>
			<div class="controls">
				<sys:treeselect id="taskOperator" name="taskOperator.id" value="${taskMain.taskOperator.id}" labelName="taskOperator.name" labelValue="${taskMain.taskOperator.name}"
					title="用户" url="/sys/office/treeData?type=3" cssClass="required" allowClear="true" notAllowSelectParent="true"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">任务标题：</label>
			<div class="controls">
				<form:input path="taskTitle" htmlEscape="false" maxlength="255" class="input-xlarge required"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">任务类型：</label>
			<div class="controls">
				<form:select path="taskType" class="input-xlarge required">
					<form:option value="" label=""/>
					<form:options items="${fns:getDictList('DEVCHK_PERIOD')}" itemLabel="label" itemValue="value" htmlEscape="false" disabled="true"/>
				</form:select>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">部门：</label>
			<div class="controls">
				<sys:treeselect id="sysOffice" name="sysOffice.id" value="${taskMain.sysOffice.id}" labelName="sysOffice.name" labelValue="${taskMain.sysOffice.name}"
					title="部门" url="/sys/office/treeData?type=2" cssClass="" allowClear="true" notAllowSelectParent="true" disabled="disabled"/>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">备注：</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="255" class="input-xxlarge "/>
			</div>
		</div>

	</form:form>
    <c:forEach items="${deviceMainList}" var="deviceMain">
        <div id="deviceMain-${deviceMain.id}" >
            <div>
                <table class="table table-bordered">
            <tr>
                <td>
                    <label>设备编码：</label>
                    <label>${deviceMain.deviceCode}</label>
                    <span id="btn-${deviceMain.id}" class="btn btn-primary btn-mini" onclick="mytoggle('${taskMain.id}','${deviceMain.id}')">显示详情</span>
                </td>
                <td>
                    <label>设备名称：</label>
                    <label>${deviceMain.deviceName}</label>
                </td>
                <td>
                    <label>所属部门：</label>
                    <label>${deviceMain.sysOffice.name}</label>
                </td>
            </tr>
            <tr>
                <td>
                    <label>设备类型：</label>
                    <label>${deviceMain.deviceType.name}</label>
                </td>
                <td>
                    <label >包机人：</label>
                    <label>${deviceMain.deviceUser.name}</label>
                </td>
                <td>
                    <label>设备图片：</label>
                    <label>
                        <c:if test="${not empty deviceMain.deviceImgUrl}">
                            <a href="${deviceMain.deviceImgUrl}" target="_blank">点击查看</a>
                        </c:if>
                        <c:if test="${empty deviceMain.deviceImgUrl}">
                            无
                        </c:if>
                    </label>
                </td>
            </tr>
            <div>
                <td>
                    <label>设备手册：</label>
                    <label>
                        <c:if test="${not empty deviceMain.deviceManualUrl}">
                            <a href="${deviceMain.deviceManualUrl}" target="_blank">${deviceMain.deviceManualName}</a>
                        </c:if>
                        <c:if test="${empty deviceMain.deviceManualUrl}">
                            无
                        </c:if>
                    </label>
                </td>
                <td colspan="2">
                    <label>备注：</label>
                    <label>${deviceMain.remarks}</label>
                </td>
            </div>
            </table>
            </div>
            <div id="devchkItem-${deviceMain.id}" style="display: none;">

            </div>
        </div>
    </c:forEach>
    <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
</body>
</html>